using LocalBoard.Controller;

namespace LocalBoard.Web.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using LocalBoard.Domain;
	using LocalBoard.Persistence;

	/// <summary>
	///		Summary description for leftnavClub.
	/// </summary>
	public partial class leftnavClub : ClubUserControl
	{

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(clubController != null)
			{
				leftNav.DataSource = clubController.GetBoardList();
				leftNav.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.leftNav.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.leftNav_ItemDataBound);

		}
		#endregion

		private void leftNav_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Header)
			{
				HyperLink clubLink = e.Item.FindControl("clubLink") as HyperLink;
				clubLink.NavigateUrl = ClubPage.AddClubQueryStringToUrl(clubLink.NavigateUrl, clubController.ClubId);
//				clubLink.Text = club.Name;
			}
			if (e.Item.DataItem != null
				&& (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item))
			{
				HyperLink boardLink = e.Item.FindControl("boardLink") as HyperLink;
				string boardPagePath = (boardLink != null)?boardLink.Attributes["boardpagepath"]:"";

				Board board = e.Item.DataItem as Board;

				if(board != null)
				{
					if(new BoardSecurity(board, clubController).CanUserAccessBoard(this.ClubUser))
					{
						boardLink.NavigateUrl = ClubPage.AddBoardQueryStringToUrl(ClubPage.AddClubQueryStringToUrl(boardPagePath, clubController.ClubId), board.Id);
						boardLink.Text = board.Name;
					}
				}
			}
			if(e.Item.ItemType == ListItemType.Footer)
			{
				HyperLink manageLink = e.Item.FindControl("manageLink") as HyperLink;
				if(clubController.GetClubSecurity().CanUserManageClub(this.ClubUser))
				{
					manageLink.NavigateUrl = ClubPage.AddClubQueryStringToUrl(manageLink.NavigateUrl, clubController.ClubId);
					manageLink.Visible = true;
				}
			}
		}
	}
}
